package com.xiaomi.web.handlers;

import com.github.pagehelper.PageInfo;
import com.xiaomi.domain.CategoryEntity;
import com.xiaomi.domain.GoodsEntity;
import com.xiaomi.service.CateService;
import com.xiaomi.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Controller
@RequestMapping({"/goods","admin"})
public class GoodsHandler {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CateService cateService;

    @RequestMapping("/findPageGoods")
    public String findPageGoods(@RequestParam(defaultValue = "1")Integer pageNum, Model model){
        PageInfo<GoodsEntity> pageInfo = goodsService.findPageGoods(pageNum);//固定每页显示5条
        model.addAttribute("pageInfo",pageInfo);

        return "admin/goods_list";
    }
    @RequestMapping("/deleteGoodsById")
    public String deleteGoodsById(Integer pageNum,String[] ids,Model model){
        boolean flag = goodsService.deleteGoodsById(ids);
        if (flag == false){
            return "admin/error";
        }

        PageInfo<GoodsEntity> pageInfo = goodsService.findPageGoods(pageNum);//固定每页显示5条
        model.addAttribute("pageInfo",pageInfo);
        return "admin/goods_list";
    }
    //goods/findGoodsById&gid=${goods.gid}
    @RequestMapping("/findGoodsById")
    public String findGoodsById(String gid, Model model){
        GoodsEntity good = goodsService.findGoodsById(gid);
        List<CategoryEntity> categories = cateService.findAllCate();

        model.addAttribute("goods",good);
        model.addAttribute("cates",categories);
        return "admin/goods_update";
    }
    //根据商品id找到对应的详情页面
    @RequestMapping("/findGoodsById1")
    public String findGoodsById1(String gid, Model model){
        GoodsEntity good = goodsService.findGoodsById(gid);
        model.addAttribute("goods",good);
        return "detail";
    }

    //goods/addGoods
    @RequestMapping("/addGoodsBefore")
    public String addGoodsBefore(Map map){
        List<CategoryEntity> categories = cateService.findAllCate();
        map.put("cates",categories);

        return "admin/goods_add";
    }

    @RequestMapping("/ajaxImg")
    @ResponseBody
    public String ajaxImg(MultipartFile pic_, HttpServletRequest request){
        String realPath = request.getServletContext().getRealPath("/img");
        File storeDirectory = new File(realPath);
        if (!storeDirectory.exists())
            storeDirectory.mkdirs();
        String originalFilename = pic_.getOriginalFilename();//原文件名：比如a.jpg
        String filename = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        try {
            pic_.transferTo(new File(storeDirectory,filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filename;
    }
    @RequestMapping("/updateGoods")
    public String updateGoods(String productDate_,GoodsEntity goods,Integer cid){
        CategoryEntity category = new CategoryEntity();
        category.setCid(cid);
        goods.setCategory(category);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            goods.setProductDate(sdf.parse(productDate_));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Boolean flag = goodsService.updateGoods(goods);
        return "forward:/goods/findPageGoods";
    }
    @RequestMapping("/addGoods")
    public String addGoods(String productDate_,GoodsEntity goods,Integer cid){
        CategoryEntity category = new CategoryEntity();
        category.setCid(cid);
        goods.setCategory(category);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            goods.setProductDate(sdf.parse(productDate_));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Boolean flag = goodsService.addGoods(goods);
        return "forward:/goods/findPageGoods";

    }
}
